//
//  LDataSource.m
//  构架模式MVC
//
//  Created by XHJ on 2020/8/24.
//  Copyright © 2020 L. All rights reserved.
//

#import "LDataSource.h"
#import "TableViewCell.h"

@interface LDataSource ()

@property (nonatomic, copy)void (^configure)(id cell, id model, NSIndexPath *indexPath);
@property (nonatomic, copy)NSString *identifier;

@end

@implementation LDataSource

- (instancetype)initWithCellIdentifier:(NSString *)identifier configure:(void (^)(id _Nonnull, id _Nonnull, NSIndexPath * _Nonnull))configure  {
    self = [super init];
    if (self) {
        _identifier = identifier;
        _configure = configure;
        _datas = [NSArray array];
    }
    return self;
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _datas.count;
}


- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.identifier];
    if (cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.identifier];
    }
    if (self.configure) {
        self.configure(cell, _datas[indexPath.row], indexPath);
    }
    return cell;
    
}
- (void)addDataArr:(NSArray *)data {
    _datas = data;
}
@end
